package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.bean.UploadFile;
import com.fwtai.bean.UploadObject;
import com.fwtai.config.ConfigFile;
import com.fwtai.config.LocalUserId;
import com.fwtai.service.AsyncService;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import com.fwtai.web.VideosDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 视频列表业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2022-03-22 11:58:24
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class VideosService{

    @Value("${upload_dir_window}")
    private String dir_window;

    @Value("${upload_dir_linux}")
    private String dir_linux;

    @Resource
    private VideosDao videosDao;

    @Resource
    private AsyncService asyncService;

    public String add(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_title = "title";
        final String p_wechat_id = "wechat_id";
        final String p_play_url = "play_url";
        final String p_domain_id = "domain_id";
        final String p_img_local = "img_local";
        final String p_details = "details";
        final String p_sort = "sort";
        final String validate = ToolClient.validateField(formData,p_title,p_wechat_id,p_play_url,p_details,p_img_local,p_sort,p_domain_id);
        if(validate != null)return validate;
        if(formData.getString(p_title).length() > 100){
            return ToolClient.createJsonFail("标题内容字数太长");
        }
        if(formData.getString(p_wechat_id).length() > 128){
            return ToolClient.createJsonFail("微信视频号的id内容字数太长");
        }
        if(formData.getString(p_play_url).length() > 512){
            return ToolClient.createJsonFail("小程序播放的url_id内容字数太长");
        }
        if(formData.getString(p_details).length() > 2048){
            return ToolClient.createJsonFail("描述内容太长");
        }
        if(formData.getString(p_img_local).length() > 384){
            return ToolClient.createJsonFail("封面上传图片全路径内容字数太长");
        }
        if(formData.getString(p_sort).length() > 19){
            return ToolClient.createJsonFail("排序号升序内容字数太长");
        }
        final String fieldInteger = ToolClient.validateInteger(formData,p_sort);
        if(fieldInteger != null)return fieldInteger;
        final String baseDir = ToolString.isLinuxOS() ? dir_linux : dir_window;
        final UploadObject uploadObject = ToolClient.uploadFile(request,baseDir,"/knowledge/video",false,1,3,true);
        final String errorMsg = uploadObject.getErrorMsg();
        if(errorMsg != null) return ToolClient.createJsonFail(errorMsg);
        final ArrayList<UploadFile> listFiles = uploadObject.getListFiles();
        if(listFiles == null || listFiles.size() <= 0) return ToolClient.createJsonFail("请选择上传文件");
        final UploadFile file = listFiles.get(0);
        formData.put("img_local",file.getFullPath());
        formData.put("img_url",file.getUrlFile());
        formData.put("kid",new IdBuilder().getId121L());
        formData.put("loginId",LocalUserId.get());
        return ToolClient.executeRows(videosDao.add(formData));
    }

    public String edit(final HttpServletRequest request){
        final PageFormData formData = new PageFormData(request);
        final String p_kid = "kid";
        final String p_title = "title";
        final String p_domain_id = "domain_id";
        final String p_wechat_id = "wechat_id";
        final String p_play_url = "play_url";
        final String p_sort = "sort";
        final String p_details = "details";
        final String p_img_local = "img_local";
        final String validate = ToolClient.validateField(formData,p_title,p_wechat_id,p_play_url,p_sort,p_details,p_kid,p_domain_id);
        if(validate != null)return validate;
        if(formData.getString(p_title).length() > 100){
            return ToolClient.createJsonFail("标题内容字数太长");
        }
        if(formData.getString(p_wechat_id).length() > 128){
            return ToolClient.createJsonFail("微信视频号的id内容字数太长");
        }
        if(formData.getString(p_play_url).length() > 512){
            return ToolClient.createJsonFail("小程序播放的url_id内容字数太长");
        }
        final String imgLocal = formData.getString(p_img_local);
        if(imgLocal != null && imgLocal.length() > 384){
            return ToolClient.createJsonFail("封面上传图片全路径内容字数太长");
        }
        if(formData.getString(p_details).length() > 2048){
            return ToolClient.createJsonFail("描述内容太长");
        }
        if(formData.getString(p_sort).length() > 19){
            return ToolClient.createJsonFail("排序号升序内容字数太长");
        }
        final String fieldInteger = ToolClient.validateInteger(formData,p_sort);
        if(fieldInteger != null)return fieldInteger;
        final String baseDir = ToolString.isLinuxOS() ? dir_linux : dir_window;
        final UploadObject uploadObject = ToolClient.uploadFile(request,baseDir,"/knowledge/video",false,1,3,false);
        final String errorMsg = uploadObject.getErrorMsg();
        if(errorMsg != null) return ToolClient.createJsonFail(errorMsg);
        final ArrayList<UploadFile> listFiles = uploadObject.getListFiles();
        if(listFiles != null && listFiles.size() > 0){
            final UploadFile file = listFiles.get(0);
            formData.put("img_local",file.getFullPath());
            formData.put("img_url",file.getUrlFile());
        }
        final String exist_key = videosDao.queryExistById(formData.getString(p_kid));
        if(exist_key == null){
            return ToolClient.createJsonFail("数据不存在,刷新重试");
        }
        return ToolClient.executeRows(videosDao.edit(formData));
    }

    public String queryById(final String kid){
        if(kid == null || kid.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        return ToolClient.queryJson(videosDao.queryById(kid));
    }

    public String delById(final String kid,final String imgLocal){
        if(kid == null || kid.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        if(imgLocal == null || imgLocal.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        final String exist_key = videosDao.queryExistById(kid);
        if(exist_key == null){
            return ToolClient.createJsonFail("删除失败,数据已不存在");
        }
        final int rows = videosDao.delById(kid);
        if(rows > 0){
            asyncService.delLocalFile(imgLocal);
        }
        return ToolClient.executeRows(rows);
    }

    public String delByKeys(final String ids){
        if(ids == null || ids.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        final ArrayList<String> lists = ToolString.keysToList(ids);
        if(lists == null || lists.size() <= 0){
            return ToolClient.createJsonFail("请选择要删除的数据");
        }
        return ToolClient.executeRows(videosDao.delByKeys(lists),"操作成功","数据不存在,刷新重试");
    }

    public String editFlag(final String kid,final String flag){
        if(kid == null || kid.length() <= 0 || flag == null || flag.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        final HashMap<String,Object> params = new HashMap<>();
        params.put("kid",kid);
        params.put("flag",flag);
        return ToolClient.executeRows(videosDao.editFlag(params));
    }

    public String listData(PageFormData formData){
        try {
            formData = ToolClient.dataTableMysql(formData);
            if(formData == null)return ToolClient.jsonValidateField();
            final HashMap<String,Object> map = videosDao.listData(formData);
            return ToolClient.dataTableOK((List<Object>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
        } catch (final Exception ignored){
            return ToolClient.dataTableException(formData.get("sEcho"));
        }
    }

    public String getDomainList(){
        return ToolClient.queryJson(videosDao.getDomainList());
    }
}